from django.core.cache import cache as default_cache
from rest_framework.throttling import SimpleRateThrottle


class IpThrottle(SimpleRateThrottle):
    cache = default_cache
    scope = 'ip'

    def get_cache_key(self, request, view):
        ident = self.get_ident(request)
        return self.cache_format % {
            'scope': self.scope,
            'ident': ident
        }


class UserThrottle(SimpleRateThrottle):
    cache = default_cache
    scope = 'user'

    def get_cache_key(self, request, view):
        ident = request.user.pk
        return self.cache_format % {
            'scope': self.scope,
            'ident': ident
        }
